/*
 * KKSpriteAnimation.cpp
 *
 *  Created on: 2013-9-15
 *      Author: wanghua
 */

#include "Classes/kaka/KKSpriteAnimation.h"

#include "KKSpriteAnimation.h"

USING_NS_CC;

KKSpriteAnimation::KKSpriteAnimation() {
	m_frames = new KKSpriteFrameArray();

}

KKSpriteAnimation::~KKSpriteAnimation() {
	CC_SAFE_DELETE(m_frames);
}

void KKSpriteAnimation::update(ccTime time) {
	//TODO implement sprite animation
}

KKSpriteAnimation* KKSpriteAnimation::animationWithXml(TiXmlElement* element) {

	KKSpriteAnimation* animation = new KKSpriteAnimation();

	if (animation->initWithXml(element)) {
		animation->retain();
		animation->release();
		return animation;
	}

	CC_SAFE_DELETE(animation);

	return NULL;

}

bool KKSpriteAnimation::initWithXml(TiXmlElement* element) {

	this->m_name = element->Attribute("name");

	TiXmlElement* frameElment = element->FirstChildElement("frame");

	KKSpriteFrame* frame = NULL;

	while (frameElment) {
		frame = KKSpriteFrame::frameWithXml(frameElment);
		if (frame) {
			this->m_frames->addObject(frame);
		}

		frameElment = frameElment->NextSiblingElement("frame");
	}

	return this->m_frames->count() > 1;
}

KKSpriteFrame* KKSpriteFrame::frameWithXml(TiXmlElement* element) {
	KKSpriteFrame* frame = new KKSpriteFrame();

	if (frame->initWithXml(element)) {
		frame->retain();
		frame->release();
		return frame;
	}

	CC_SAFE_DELETE(frame);

	return NULL;
}

bool KKSpriteFrame::initWithXml(TiXmlElement* element) {

	this->m_duration = atof(element->Attribute("duration")) / 1000;
	this->m_image = element->Attribute("image");

	return true;
}
